﻿using System;
using System.Collections.Generic;
using System.Text;
using RuoTeng.Util;
using RTGather.Lib.gather;

namespace RTGather.Lib
{
   public class GatherCJ : Task
    {


        private List<string> urlList = new List<string>();///URL列表

        public string filename = "";

        public string keyword = "";

        public string vcode = "";
        public string format = "";


      
        public int count = 0;

        private List<Company> companyList = new List<Company>();//公司列表


        public delegate void LogHandle(string text, System.Drawing.Color color);

        public event LogHandle Log;

        public string Format
        {
            get
            {
                if (format  == "直版")
                {
                    return "1";
                }
                else
                {
                    return "0";

                }
            }
        }
        protected override void OnStart()
        {
            base.OnStart();
            this.CallMessage("任务开始！", System.Drawing.Color.Green);
            fbb_cj f = new fbb_cj();


            int pagenum =  f.getPageCount(count);
            //-------------------------------------------------------------------------------------------------
            int i = 0;
           for(int y=1;y<=pagenum;y++)
            {
                string url = f.getUrl(keyword, vcode, y);
               this.CallMessage("开始解析" + url, System.Drawing.Color.Green);
                f.getData(url,vcode,Format);
                i++;
                this.CallMessage("解析结束" + url, System.Drawing.Color.Green);
                this.CallMessage("还剩余" + (pagenum - i) + "条", System.Drawing.Color.Green);
                this.CallMessage("采集任务结束", System.Drawing.Color.Green);
                this.CallMessage("开始生成文件,请耐心等待", System.Drawing.Color.Green);
                f.ExcelExport(filename);
                this.CallMessage("文件生成结束", System.Drawing.Color.Green);
            }

            this.CallMessage("流程结束", System.Drawing.Color.Green);
            this.Stop();
            //-------------------------------------------------------------------------------------------------
            if (this.ToChangeState == RuoTeng.Util.Task.TaskState.Pause)
            {
                this.OnPause();
            }
            else if (this.ToChangeState == RuoTeng.Util.Task.TaskState.Stop)
            {
                this.OnStop();
            }

        }
        protected override void OnPause()
        {
            base.OnPause();
            this.CallMessage("任务暂停！", System.Drawing.Color.Red);
        }

        protected override void OnStop()
        {
            base.OnStop();
        }

        private void CallMessage(string message, System.Drawing.Color color)
        {
            if (this.Log != null)
            {
                this.Log(message, color);
            }
        }


    }
}
